package com.hxkcloud.patient.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.activitys.InputXuetangActivity;
import com.hxkcloud.patient.activitys.InputXueyaActivity;
import com.hxkcloud.patient.activitys.XuetangDetailActivity;
import com.hxkcloud.patient.activitys.XueyaDetailActivity;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * Created by william_sun on 2016-07-06.
 * Description:监测模块
 */
public class MonitorFragment extends BaseFragment implements View.OnClickListener {

    private View view;

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private LinearLayout ll_empty;
    private LinearLayout ll_info;

    private LineChartView chart;
    private LineChartData data;
    private int numberOfLines = 1;
    private int maxNumberOfLines = 4;
    private int numberOfPoints = 5;

    float[][] randomNumbersTab = new float[maxNumberOfLines][numberOfPoints];

    private boolean hasAxes = true;
    private boolean hasAxesNames = true;
    private boolean hasLines = true;
    private boolean hasPoints = true;
    private ValueShape shape = ValueShape.CIRCLE;
    private boolean isFilled = false;
    private boolean hasLabels = false;
    private boolean isCubic = false;
    private boolean hasLabelForSelected = false;
    private boolean pointsHaveDifferentColor;


    private LineChartView second_chart;
    private LineChartData second_data;
    private int sNumberOfLines = 2;
    private int sMaxNumberOfLines = 4;
    private int sNumberOfPoints = 5;
    float[][] sRandomNumbersTab = new float[sMaxNumberOfLines][sNumberOfPoints];


    private Button xuetangBtn;
    private Button xueyaBtn;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_monitor_chart, container, false);


        chart = (LineChartView) view.findViewById(R.id.chart);
        second_chart = (LineChartView) view.findViewById(R.id.second_chart);
//        chart.setOnValueTouchListener(new ValueTouchListener());

        ll_title = (LinearLayout) view.findViewById(R.id.ll_title);
        iv_back = (ImageView) view.findViewById(R.id.iv_title_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.INVISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(getResources().getText(R.string.monitor_text));

        // chart
        generateValues();
        generateData();
        chart.setViewportCalculationEnabled(false);
        resetViewport();

        //chart2
        generateValues2();
        generateData2();
        second_chart.setViewportCalculationEnabled(false);
        resetViewport2();

        initView();
//        initData();
        initListener();

        return view;
    }

    @Override
    protected void initView() {
        xuetangBtn = (Button) view.findViewById(R.id.xuetang_btn_input);
        xueyaBtn = (Button) view.findViewById(R.id.xueya_btn_input);

        ll_empty = (LinearLayout) view.findViewById(R.id.ll_department_empty);
        ll_info = (LinearLayout) view.findViewById(R.id.ll_monitorchart_info);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(loginInfo.getDepartmentId())) {
            ll_empty.setVisibility(View.VISIBLE);
            ll_info.setVisibility(View.GONE);
        } else {
            initData();
            ll_empty.setVisibility(View.GONE);
            ll_info.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initListener() {
        xuetangBtn.setOnClickListener(this);
        xueyaBtn.setOnClickListener(this);
        chart.setOnClickListener(this);
        second_chart.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    private void generateValues() {
        for (int i = 0; i < maxNumberOfLines; ++i) {
            for (int j = 0; j < numberOfPoints; ++j) {
                randomNumbersTab[i][j] = (float) Math.random() * 10f;
            }
        }
    }

    private void generateData() {

        List<Line> lines = new ArrayList<Line>();
        for (int i = 0; i < numberOfLines; ++i) {

            List<PointValue> values = new ArrayList<PointValue>();
//                for (int j = 0; j < numberOfPoints; ++j) {
//                    PointValue point =new PointValue(j, randomNumbersTab[i][j]);
//                    if(j%2==0){
//                        //修改了PointValue，LineChartRenderer
//                        point.setBig(true);
//                    }
//                    values.add(point);
//                }
            values.add(new PointValue(0, 2));
            values.add(new PointValue(1, 8));
            PointValue point = new PointValue(2, 1);
            point.setBig(true);
            values.add(point);
            values.add(new PointValue(3, 6));

            Line line = new Line(values);
            line.setColor(ChartUtils.COLORS[i]);
            line.setShape(shape);
            line.setCubic(isCubic);
            line.setFilled(isFilled);
            line.setHasLabels(hasLabels);
            line.setHasLabelsOnlyForSelected(hasLabelForSelected);
            line.setHasLines(hasLines);
            line.setHasPoints(hasPoints);
            if (pointsHaveDifferentColor) {
                line.setPointColor(ChartUtils.COLORS[(i + 1) % ChartUtils.COLORS.length]);
            }
            lines.add(line);
        }

        data = new LineChartData(lines);

        if (hasAxes) {
            List<AxisValue> mAxisValues = new ArrayList<AxisValue>();
            mAxisValues.add(new AxisValue(0).setLabel("06:00")); //为每个对应的i设置相应的label(显示在X轴)
            mAxisValues.add(new AxisValue(1).setLabel("12:00"));
            mAxisValues.add(new AxisValue(2).setLabel("18:00"));
            mAxisValues.add(new AxisValue(3).setLabel("24:00"));

            Axis axisX = new Axis(mAxisValues);

            List<AxisValue> yAxisValues = new ArrayList<AxisValue>();
            yAxisValues.add(new AxisValue(2).setLabel("02")); //为每个对应的i设置相应的label(显示在X轴)
            yAxisValues.add(new AxisValue(4).setLabel("04"));
            yAxisValues.add(new AxisValue(6).setLabel("06"));
            yAxisValues.add(new AxisValue(8).setLabel("08"));
            yAxisValues.add(new AxisValue(10).setLabel("10"));

            Axis axisY = new Axis(yAxisValues).setHasLines(true);
            if (hasAxesNames) {
//                    axisX.setName("Axis X");
//                    axisY.setName("Axis Y");
            }
            data.setAxisXBottom(axisX);
            data.setAxisYLeft(axisY);
        } else {
            data.setAxisXBottom(null);
            data.setAxisYLeft(null);
        }

//            data.setBaseValue(Float.NEGATIVE_INFINITY);
        chart.setLineChartData(data);

    }

    private void generateValues2() {
        for (int i = 0; i < sMaxNumberOfLines; ++i) {
            for (int j = 0; j < sNumberOfPoints; ++j) {
                sRandomNumbersTab[i][j] = (float) Math.random() * 10f;
            }
        }
    }

    private void resetViewport() {
        // Reset viewport height range to (0,100)
        final Viewport v = new Viewport(chart.getMaximumViewport());
        v.bottom = 0;
        v.top = 10;
        v.left = 0;
        v.right = numberOfPoints - 1;
        chart.setMaximumViewport(v);
        chart.setCurrentViewport(v);
    }


    private void resetViewport2() {
        // Reset viewport height range to (0,100)
        final Viewport v = new Viewport(chart.getMaximumViewport());
        v.bottom = 0;
        v.top = 180;
        v.left = 0;
        v.right = numberOfPoints - 1;
        second_chart.setMaximumViewport(v);
        second_chart.setCurrentViewport(v);
    }

    private void generateData2() {

        List<Line> lines = new ArrayList<Line>();
        for (int i = 0; i < sNumberOfLines; ++i) {
            if (i == 0) {
                List<PointValue> values = new ArrayList<PointValue>();
                values.add(new PointValue(0, 65));
                values.add(new PointValue(1, 88));
                PointValue point = new PointValue(2, 95);
                point.setBig(true);
                values.add(point);
                values.add(new PointValue(3, 80));

                Line line = new Line(values);
                line.setColor(ChartUtils.COLORS[i]);
                line.setShape(shape);
                line.setCubic(isCubic);
                line.setFilled(isFilled);
                line.setHasLabels(hasLabels);
                line.setHasLabelsOnlyForSelected(hasLabelForSelected);
                line.setHasLines(hasLines);
                line.setHasPoints(hasPoints);
                if (pointsHaveDifferentColor) {
                    line.setPointColor(ChartUtils.COLORS[(i + 1) % ChartUtils.COLORS.length]);
                }
                lines.add(line);
            } else if (i == 1) {

                List<PointValue> values = new ArrayList<PointValue>();
                values.add(new PointValue(0, 140));
                values.add(new PointValue(1, 130));
                PointValue point = new PointValue(2, 160);
                point.setBig(true);
                values.add(point);
                values.add(new PointValue(3, 135));

                Line line = new Line(values);
                line.setColor(ChartUtils.COLORS[i]);
                line.setShape(shape);
                line.setCubic(isCubic);
                line.setFilled(isFilled);
                line.setHasLabels(hasLabels);
                line.setHasLabelsOnlyForSelected(hasLabelForSelected);
                line.setHasLines(hasLines);
                line.setHasPoints(hasPoints);
                if (pointsHaveDifferentColor) {
                    line.setPointColor(ChartUtils.COLORS[(i + 1) % ChartUtils.COLORS.length]);
                }
                lines.add(line);
            }

        }


        second_data = new LineChartData(lines);

        if (hasAxes) {
            List<AxisValue> mAxisValues = new ArrayList<AxisValue>();
            mAxisValues.add(new AxisValue(0).setLabel("06:00")); //为每个对应的i设置相应的label(显示在X轴)
            mAxisValues.add(new AxisValue(1).setLabel("12:00"));
            mAxisValues.add(new AxisValue(2).setLabel("18:00"));
            mAxisValues.add(new AxisValue(3).setLabel("24:00"));

            Axis axisX = new Axis(mAxisValues);

            List<AxisValue> yAxisValues = new ArrayList<AxisValue>();
            yAxisValues.add(new AxisValue(60).setLabel("60")); //为每个对应的i设置相应的label(显示在X轴)
            yAxisValues.add(new AxisValue(90).setLabel("90"));
            yAxisValues.add(new AxisValue(120).setLabel("120"));
            yAxisValues.add(new AxisValue(150).setLabel("150"));
            yAxisValues.add(new AxisValue(180).setLabel("180"));

            Axis axisY = new Axis(yAxisValues).setHasLines(true);
            if (hasAxesNames) {
//                    axisX.setName("Axis X");
//                    axisY.setName("Axis Y");
            }
            second_data.setAxisXBottom(axisX);
            second_data.setAxisYLeft(axisY);
        } else {
            second_data.setAxisXBottom(null);
            second_data.setAxisYLeft(null);
        }

//            data.setBaseValue(Float.NEGATIVE_INFINITY);
        second_chart.setLineChartData(second_data);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.xuetang_btn_input:
                Intent xuetangIntent = new Intent(getActivity(), InputXuetangActivity.class);
                startActivity(xuetangIntent);
                break;
            case R.id.xueya_btn_input:
                Intent xueyaIntent = new Intent(getActivity(), InputXueyaActivity.class);
                startActivity(xueyaIntent);
                break;
            case R.id.chart://血糖详情页
                Intent xuetang1Intent = new Intent(getActivity(), XuetangDetailActivity.class);
                startActivity(xuetang1Intent);
                break;
            case R.id.second_chart://血压详情页
                Intent xueya1Intent = new Intent(getActivity(), XueyaDetailActivity.class);
                startActivity(xueya1Intent);
                break;
        }
    }
}
